package com.sgb.zhaocai.widget.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;
import com.sgb.link.utils.NumericalTransformationTool;
import com.sgb.zhaocai.BR;
import com.sgb.zhaocai.R;
import com.sgb.zhaocai.entity.PickExpertDetailEntity;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.view.selection.CommonPopEntity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 专家库-评标专业-适配器
 */
public class EvaluationProfessionalAdapter extends BaseRecycleAdapter<PickExpertDetailEntity.Majors> {

    private Callback callback;
    private Context context;
    private List<CommonPopEntity> professionalData = new ArrayList<>();
    private List<List<CommonPopEntity>> typeData = new ArrayList<>();
    private String professionalName;
    private String typeName = "";
    private List<PickExpertDetailEntity.Majors> list;
    private Calendar selectedDate;
    private boolean firstDelete;

    public EvaluationProfessionalAdapter(Context context, List<PickExpertDetailEntity.Majors> list) {
        super(context, R.layout.item_evaluation_professional, list);
        this.context = context;
        this.list = list;
        selectedDate = Calendar.getInstance();
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, @SuppressLint("RecyclerView") int position) {
        super.onBindViewHolder(holder, position);
        ImageView delete = holder.itemView.findViewById(R.id.delete);//删除
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callback.delete(position);
            }
        });
        TextView tvSort = holder.itemView.findViewById(R.id.tv_sort);
        if(firstDelete){
            tvSort.setText("(" + NumericalTransformationTool.getNumberText(position + 1) + ")");
        }else{
            tvSort.setText("(" + NumericalTransformationTool.getNumberText(position + 2) + ")");
        }
        ItemInputTextView tvProfessional = holder.itemView.findViewById(R.id.tv_professional);
        ItemInputTextView tvStartYear = holder.itemView.findViewById(R.id.tv_start_year);
        tvStartYear.setDeliverLineVG(false);

        tvStartYear.setOnWholeItemClickListener(new ItemInputTextView.OnWholeItemClickListener() {
            @Override
            public void OnWholeItemClick(View view) {
                TimePickerView timePicker = new TimePickerBuilder(context, (date, v) -> {
                    @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
                    String format = sdf.format(date);
                    tvStartYear.setContent(format);
                    list.get(position).setStartYear(format);
                }).setType(new boolean[]{true, false, false, false, false, false})
                        .setCancelText("取消").setSubCalSize(15)
                        .setCancelColor(Color.parseColor("#666666"))
                        .setTitleText("从事该专业起始年份")
                        .setTitleColor(Color.parseColor("#969696")).setTitleSize(15)
                        .setSubmitText("确定")
                        .setRangDate(null, selectedDate)
                        .setSubmitColor(Color.parseColor("#5792FD"))
                        .build();
                timePicker.show();
            }
        });

        tvProfessional.setOnWholeItemClickListener(new ItemInputTextView.OnWholeItemClickListener() {
            @Override
            public void OnWholeItemClick(View view) {
                List<String> string = new ArrayList<>();
                if (professionalData.size() == 0) return;
                OptionsPickerView businessOption = new OptionsPickerBuilder(context, (options1, options2, options3, v) -> {
                    professionalName = professionalData.get(options1).getTypeName();
                    string.add(professionalData.get(options1).getNo());
                    if (typeData.size() == 0 || null == typeData.get(options1) || typeData.get(options1).size() == 0 || typeData.get(options1).get(options2) == null) {

                    } else {
                        typeName = typeData.get(options1).get(options2).getTypeName();
                        string.add(typeData.get(options1).get(options2).getNo());
                    }
                    if (!TextUtils.isEmpty(typeName)) {
                        tvProfessional.setContent(professionalName + "/" + typeName);
                        list.get(position).setMajorName(professionalName + "/" + typeName);
                    } else {
                        tvProfessional.setContent(professionalName);
                        list.get(position).setMajorName(professionalName);
                    }
                    list.get(position).setMajor(string);
                }).build();
                businessOption.setPicker(professionalData, typeData);
                businessOption.setSelectOptions(0, 0);
                businessOption.show();
            }
        });

        if(list.get(position) != null){
            tvProfessional.setContent(list.get(position).getMajorName());
            tvStartYear.setContent(list.get(position).getStartYear());
        }
    }

    @Override
    public void convert(ItemViewHolder holder, PickExpertDetailEntity.Majors entity) {
        holder.setBinding(BR.viewModel, entity);
    }

    public interface Callback {
        void delete(int position);
    }

    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    public void setCertificateData(List<CommonPopEntity> data, List<List<CommonPopEntity>> childData) {
        professionalData = data;
        typeData = childData;
    }

    public void getFirstDelete(boolean isDelete) {
        firstDelete = isDelete;
        if (firstDelete) {
            notifyDataSetChanged();
        }
    }
}
